


import axios from 'axios';

const config = {
	//baseURL: 'http://116.148.228.110:8096/Admin/CS/ServerAPIData.ashx',
	baseURL: '',
	timeout: 3000,
	headers: {
		'Content-Type': 'application/x-www-form-urlencoded'
	},
};

const api = axios.create(config);

// 默认 post 请求，使用 application/json 形式
api.defaults.headers.post['Content-Type'] = 'application/json';


//封装下post
/**
 * @param {Object} url  server
 * @param {Object} params  地址参数string
 * @param {Object} dat 数据
 */
api.post = function (url, params, dat) {

	let u = (url || config.baseURL) + (params ? '?' + params : '');
	return new Promise((resolve, reject) => {
		//console.log("****************************");
		axios({
			method: 'POST',
			url: u,
			data: JSON.stringify(dat),
			params,
			headers: {
				'Content-Type': 'application/json; charset=utf-8',
			},
		}).then(response => {
			if (response.status == 200) {				
				//根据实际情况进行更改
				var result = response.data;
				resolve(result.data||result);
			} else {
				reject(response)
			}
		})
	});
}


//封装get
api.get = function (url, params, dat) {
	let u = (url || config.baseURL) + (params ? '?' + params : '');
	return new Promise((resolve, reject) => {
		//console.log("****************************");
		axios({
			method: 'get',
			url: u,
			params,
			headers: {
				'Content-Type': 'application/json; charset=utf-8',
			},
			// headers: {
			// 	///...config.headers
			// 	token: 'eyJhbGciOiJIUzUxMiJ9.eyJlbXBJZCI6MjIyLCJzdWIiOiJ5b3UiLCJlbXBOYW1lIjoi5qOA5rWL5ZGYMjIxMyIsInJvbGVzIjoiIiwicmlnaHRzIjoiIiwiaXNzIjoiaHpseSIsImxvY2siOjAsImJJZCI6MSwiaWF0IjoxNjUzODk4MzQ5LCJtYWMiOiIiLCJqdGkiOiIxMjM0NSJ9.00m2GdrsFEHzu9FvOp_mzKYAokkJxdZOYLWGf4f2hb-Llt6xHCLkxUR3ugmErFmp1BroAoQe8f097bw3QHme5g'
			// },
		}).then(response => {
			console.log(response);
			if (response.status == 200) {
				//根据实际情况进行更改
				//
				var result = response.data;
				resolve(result.data || result);
			} else {
				reject(response)
			}
		})
	});
}

// http response 拦截器
api.interceptors.response.use(response => {
	//拦截响应，做统一处理
	if (response.data.code) {
		// console.log(response.status);
		switch (response.status) {
			case 301:
				console.log('登录过期');
			// store.state.isLogin = false
			// router.replace({
			//   path: 'login',
			//   query: {
			//     redirect: router.currentRoute.fullPath
			//   }
			// })
		}
	}
	return response
}, error => { //接口错误状态处理，也就是说无响应时的处理
	return Promise.reject(error.response.status) // 返回接口返回的错误信息
});


/**
 * 同步登录
 */
const hLogin = async (user, back) => {
	var dat;
	await api.get(null, user).then((result) => {
		/*
		result
		
		config: {transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ƒ, …}
		data:
		headers: {cache-control: 'private', content-length: '4685', content-type: 'text/plain; charset=utf-8'}
		request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
		status: 200
		statusText: "OK"*/

		dat = result; //.data;
	});

	var d = {
		result: dat.length == 1,
		dat: null
	};
	if (d.result) {
		console.log("同步登录：", dat);
		d.dat = dat[0];
		var tick = Date.now() - new Date(d.dat.EndDate);
		if (tick >= 0) {
			d.result = false;
			d.dat = "试用时间已到!请联系管理员";
		}

	} else {
		d.dat = "请检查用户名与密码";
	}
	//console.log("同步登录：", d);

	back(d);
}

/**
 * 
 */
const hDoGet = (url, params, dat, back) => {
	//console.log(dat);
	api.get(url, params, dat).then(back);
}
/**
 * url params data
 */
const hDoPost = (u, p, d, back) => {

	//console.log(dat);
	api.post(u, p, d).then(back);
}
const hDoGet2 = (url, params, dat, back) => {
	let u = (url || config.baseURL) + (params ? '?' + params : '');
	return new Promise((resolve, reject) => {
		//console.log("****************************");
		axios({
			method: 'get',
			url: u,
			params,
			// headers: {
			// 	'Content-Type': 'application/json; charset=utf-8',
			// },
			// headers: {
			// 	///...config.headers
			// 	token: 'eyJhbGciOiJIUzUxMiJ9.eyJlbXBJZCI6MjIyLCJzdWIiOiJ5b3UiLCJlbXBOYW1lIjoi5qOA5rWL5ZGYMjIxMyIsInJvbGVzIjoiIiwicmlnaHRzIjoiIiwiaXNzIjoiaHpseSIsImxvY2siOjAsImJJZCI6MSwiaWF0IjoxNjUzODk4MzQ5LCJtYWMiOiIiLCJqdGkiOiIxMjM0NSJ9.00m2GdrsFEHzu9FvOp_mzKYAokkJxdZOYLWGf4f2hb-Llt6xHCLkxUR3ugmErFmp1BroAoQe8f097bw3QHme5g'
			// },
		}).then(response => {
			console.log(response);
			// if (response.status == 200) {
			//根据实际情况进行更改
			resolve(response)
			// } else {
			// 	reject(response)
			// }
		})
	}).then((t) => {
		back(t);
	});
}

const apis = (basicHost) => {
	config.baseURL = basicHost || config.baseURL;
	return {
		hLogin,
		hDoGet,
		hDoPost,
		hDoGet2
	}
}
export default apis;